const app = getApp()
const { editApi } = require('../../../api/services/enterpriseDangers/index')
const util = require('../../../utils/util.js')
Page({
  data: {
    loading: false,
    fileList: [],
    item: {},
    classId: '',
    show: false,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return value
    },
  },

  //跳转问题点页面
  handleTroubleState() {
    wx.navigateTo({ url: '/pages/common/troubleList/troubleList?classId=' + this.data.classId })
  },

  // 隐患图：图片上传
  afterRead(event) {
    this.setData({ loading: true })

    const { file } = event.detail
    console.log(file)
    let _this = this
    wx.uploadFile({
      url: 'https://dy.microzl.com/sq-smart-campus-api/xt-system/sysfile/uploadFile', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      header: {
        'Content-Type': 'multipart/form-data',
        Authorization: 'Bearer ' + wx.getStorageSync('token'),
      },

      success(res) {
        console.log(res)
        _this.setData({
          fileList: [],
        })

        let data = JSON.parse(res.data)
        _this.setData({ loading: false })

        _this.setData({ fileList: [{ url: data.data.fUrl }], 'item.checkUrl': data.data.fUrl })
      },
      fail(err) {
        _this.setData({ loading: false })
        console.log(err)
      },
    })
  },

  showPopup() {
    this.setData({ show: true })
  },
  // 时间弹窗
  onCloseTime() {
    this.setData({ show: false })
  },

  handleImg(event) {
    // this.setData({ showImg: true })
    let { img } = event.currentTarget.dataset
    console.log(img)
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img], // 需要预览的图片http链接列表
    })
  },

  onCloseImg() {
    this.setData({ showImg: false })
  },

  // 时间
  handleConfirm(event) {
    this.setData({
      'item.okDate': util.formatTime(new Date(event.detail)),
      show: false,
    })
  },

  onChange(event) {
    this.setData({
      'item.isOk': event.detail,
    })
  },

  onChangeOkContent(event) {
    this.setData({ 'item.okContent': event.detail })
  },
  onChangeCheckBasis(event) {
    this.setData({ 'item.checkBasis': event.detail })
  },
  onChangeTroubleSite(event) {
    this.setData({ 'item.troubleSite': event.detail })
  },
  onChangeTroubleSuggest(event) {
    this.setData({ 'item.troubleSuggest': event.detail })
  },

  // 编辑按钮
  async handleEdit() {
    this.setData({ 'item.isOk': 2 })
    console.log(this.data.item)
    const res = await editApi(this.data.item)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    let pages = getCurrentPages()
    let prevPage = pages[pages.length - 2] //上一个页面

    prevPage.setData({ editComUrl: false }) //设置排查人签名状态

    app.toast('修改成功！')
    wx.navigateBack({
      delta: 1, //返回上一级页面
    })
  },

  // 隐患类型：
  handleType() {
    wx.navigateTo({
      url: '/pages/safety/spTroubleType/spTroubleType',
    })
  },
  // 隐患分类：
  handleClass() {
    wx.navigateTo({ url: '/pages/safety/spTroubleClass/spTroubleClass' })
  },

  onLoad: function (options) {
    let listObj = JSON.parse(options.item)
    this.setData({
      item: listObj,
    })

    if (listObj.checkUrl) {
      this.setData({
        'fileList[0].url': listObj.checkUrl,
      })
    }

    let userInfo = wx.getStorageSync('userInfo')
    let userType = userInfo.userType
    this.setData({ userType: userType })
  },

  onShow: function () {
    console.log('onshow')
    console.log('classId', this.data.classId)
  },
})
